package com.ruoyi.workbench.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.DeptConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.workbench.domain.WbLeafUnit;
import com.ruoyi.workbench.domain.WbPhysicalUnit;
import com.ruoyi.workbench.service.IWbLeafLeafUnitService;
import com.ruoyi.workbench.service.IWbPhysicalUnitService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 叶子单元Controller
 *
 * @author hanjiafeng
 * @date 2021-02-24
 */
@Controller
@RequestMapping("/workbench/leaf_unit_city_8")
public class WbLeafUnitCity8Controller extends BaseController {
    private final String prefix = "workbench/leaf_unit_city_8";
    private final IWbLeafLeafUnitService wbLeafLeafUnitService;
    private final ISysDeptService sysDeptService;
    private final IWbLeafLeafUnitService leafLeafUnitService;
    private final IWbPhysicalUnitService wbPhysicalUnitService;

    public WbLeafUnitCity8Controller(IWbLeafLeafUnitService wbLeafLeafUnitService, ISysDeptService sysDeptService, IWbLeafLeafUnitService leafLeafUnitService, IWbPhysicalUnitService wbPhysicalUnitService) {
        this.wbLeafLeafUnitService = wbLeafLeafUnitService;
        this.sysDeptService = sysDeptService;
        this.leafLeafUnitService = leafLeafUnitService;
        this.wbPhysicalUnitService = wbPhysicalUnitService;
    }

    @RequiresPermissions("workbench:leaf_unit_city:view")
    @GetMapping()
    public String leafUnitCity8() {
        return prefix + "/leaf_unit_city_8";
    }

    /**
     * 查询叶子单元列表
     */
    @RequiresPermissions("workbench:leaf_unit_city:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam("id") Long id) {
        startPage();

        List<WbPhysicalUnit> physicalUnitList = wbPhysicalUnitService.selectChildrenByParentId(id);
        return getDataTable(physicalUnitList);
    }

    @PostMapping("/selectChildrenUnitByParentId")
    @ResponseBody
    public List<WbLeafUnit> selectChildrenUnitByParentId(HttpServletRequest httpServletRequest) {
        Long parentId = null;
        try {
            parentId = Long.valueOf(httpServletRequest.getParameter("parentId"));
        } catch (NumberFormatException e) {
            logger.info("无单位id");
        }
        return wbLeafLeafUnitService.selectChildrenUnitByParentId(parentId);
    }

    /**
     * 新增叶子单元
     */
    @GetMapping("/add")
    public String add(@RequestParam(name = "id") Long unit7Id, ModelMap modelMap) {
        WbPhysicalUnit physicalUnit = new WbPhysicalUnit();
        physicalUnit.setId(unit7Id);
        modelMap.put("physicalUnit", physicalUnit);
        return prefix + "/add";
    }

    /**
     * 新增保存叶子单元
     */
    @RequiresPermissions("workbench:leaf_unit_city:add")
    @Log(title = "所辖村庄", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("id") Long unit7Id, @RequestParam("unit8Name") String unit7Name) {
        WbPhysicalUnit unit7 = wbPhysicalUnitService.selectByPrimaryKey(unit7Id);
        //祖先id
        String ancestors = unit7.getAncestors() + "," + unit7Id;
        //父id
        WbPhysicalUnit unit8 = new WbPhysicalUnit();
        unit8.setParentId(unit7Id);
        unit8.setName(unit7Name);
        unit8.setAncestors(ancestors);
        unit8.setLevel(DeptConstants.UNIT_LEVEL_8);
        unit8.setType(DeptConstants.CITY_FLAG);

        return toAjax(wbPhysicalUnitService.insertWbPhysicalUnit(unit8));
    }

    /**
     * 修改叶子单元
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap modelMap) {
        WbPhysicalUnit wbPhysicalUnit = wbPhysicalUnitService.selectByPrimaryKey(id);
        modelMap.put("wbPhysicalUnit", wbPhysicalUnit);
        return prefix + "/edit";
    }

    /**
     * 修改保存叶子单元
     */
    @RequiresPermissions("workbench:leaf_unit_city:edit")
    @Log(title = "所辖村庄", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WbPhysicalUnit wbPhysicalUnit) {
        return toAjax(wbPhysicalUnitService.updateByPrimaryKeySelective(wbPhysicalUnit));
    }

    /**
     * 删除叶子单元
     */
    @RequiresPermissions("workbench:leaf_unit_city:remove")
    @Log(title = "所辖村庄", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wbLeafLeafUnitService.deleteWbLeafUnitByIds(ids));
    }

    /**
     * 选择登录名领导最高级的部门
     *
     * @param type 当level为3时，筛选标记，0：全部，1：城市营业部，2：农村营业部
     * @return 部门
     */
    @PostMapping("/getMyDept")
    @ResponseBody
    public List<SysDept> getMyDept(Byte type) {
        SysUser currentUser = ShiroUtils.getSysUser();
        return sysDeptService.selectChildrenDeptByUser(currentUser, type);
    }

    /**
     * 根据七级单元id选择8级单元
     *
     * @param unit7Id unit7Id
     * @return 八级
     */
    @GetMapping("/towerList")
    @ResponseBody
    public List<WbLeafUnit> towerList(@RequestParam(name = "id") Long unit7Id) {
        return leafLeafUnitService.selectChildrenUnitByParentId(unit7Id);
    }
}
